﻿using CardPlay.Model.CardModel;
using CardPlay.Model.GameModel;
using CardPlay.Model.GameModel.Presentation;
using CardPlay.Model.Layout;
using CardPlay.Model.Primitives;

namespace CardPlay.DemoPack.Games.BeleaguredCastleVariants
{
    [Game("72838B4E-96DF-433D-97FD-D8984E43D62F", LevelOfDifficulty.Medium, LevelOfSkill.MostlySkill, DurationOfPlay.Within5To10Minutes, Family = FamilyName)]
    public class BeleagueredCastle : StandardCardGame
    {
        public const string FamilyName = "Beleaguered Castle";

        public BeleagueredCastle()
        {
            TotalFoundations = 4;
            TotalTableaus = 8;
            TableauBuildSequence = CardFaceSequence.AnySuitDescendingRank;
        }

        public override ILayoutSite CreatePresentation(IPresentationContext presentation)
        {
            var minTableauSize = new Size(presentation.Metrics.CardSize.Width * 4, presentation.Metrics.CardSize.Height);

            var root = new SideBySideLayout
            {
                Direction = SpreadDirection.Horizontal,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                VerticalContentAlignment = VerticalAlignment.Stretch,
                MinimumSpacing = presentation.Metrics.MinSpacingAcross
            };

            {
                var column = CreateColumnLayout(presentation);
                root.Add(column);

                var style = new VisualTraits
                {
                    EmptyCue = () => EmptyPileCue.Outlined,
                    Arrangement = CardArrangement.StackRightToLeft,
                    HorizontalAlignment = HorizontalAlignment.Right,
                    ContentFit = ContentFit.FitBounds
                };

                column.MinimumSize = minTableauSize;
                presentation.AddPiles(column, new[] { Tableau1, Tableau2, Tableau3, Tableau4 }, style);
            }

            {
                var column = CreateColumnLayout(presentation);
                root.Add(column);

                presentation.AddFoundationPiles(this, column, FoundationBaseRank);
            }

            {
                var column = CreateColumnLayout(presentation);
                root.Add(column);

                var style = new VisualTraits
                {
                    EmptyCue = () => EmptyPileCue.Outlined,
                    Arrangement = CardArrangement.StackLeftToRight,
                    ContentFit = ContentFit.FitBounds
                };

                column.MinimumSize = minTableauSize;
                presentation.AddPiles(column, new[] { Tableau5, Tableau6, Tableau7, Tableau8 }, style);
            }

            return root;
        }

        protected override void PrepareDealScript(DealScript script)
        {
            script.DealAcesTo(NamesOfFoundations);
            script.DealToEach(NamesOfTableaus, "6 up");
        }

        private static SideBySideLayout CreateColumnLayout(IPresentationContext presentation)
        {
            return new SideBySideLayout
            {
                Direction = SpreadDirection.Vertical,
                MinimumSpacing = presentation.Metrics.MinSpacingDown,
                VerticalContentAlignment = VerticalAlignment.Top,
            };
        }
    }
}